package com.sam.im.heiqun.uis.activities;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;

import com.flyco.tablayout.SlidingTabLayout;
import com.flyco.tablayout.listener.OnTabSelectListener;
import com.sam.im.heiqun.R;
import com.sam.im.heiqun.nets.PGService;
import com.sam.im.heiqun.uis.fragments.LuckyRedEnvelopeFragment;
import com.sam.im.heiqun.uis.fragments.SendRedGroupPSQFragment;
import com.sam.im.heiqun.uis.fragments.SendRedGroupPTFragment;
import com.sam.im.heiqun.uis.fragments.SendRedKLFragment;
import com.sam.im.heiqun.uis.fragments.SendRedPTFragment;
import com.yuyh.library.utils.EventBusType;
import com.sam.im.heiqun.utils.EventBusTypeObject;
import com.sam.im.heiqun.utils.ToolsUtils;
import com.yuyh.library.nets.callbacks.AbsAPICallback;
import com.yuyh.library.nets.exceptions.ApiException;
import com.yuyh.library.uis.activitys.BaseSwipeBackActivity;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;

/**
 * Created by wangguodong on 2017/8/25.
 */

public class SendRedPacketSelectActivity extends BaseSwipeBackActivity implements OnTabSelectListener {

    private ArrayList<Fragment> mFragments = new ArrayList<>();
    private String[] mTitles ;

//        "拼手气红包", "普通红包", "口令红包"

    MyPagerAdapter mAdapter ;
    private long destid ;
    private int type = 0 ;//0-私聊；1-群聊

    PGService mPgService;
    public double balance = 0;

    public static void start(Activity activity, int requstCode, long destid, int type){
        Intent intent = new Intent(activity,SendRedPacketSelectActivity.class);
        intent.putExtra("destid", destid);
        intent.putExtra("type", type);
        activity.startActivityForResult(intent,requstCode);
    }

    @Override
    protected String getTitleText() {
        return getResources().getString(R.string.title_ordinary_red_packet);
    }

    @Override
    protected void initViews(Bundle savedInstanceState) {
        mPgService = PGService.getInstance();
        destid = getIntent().getLongExtra("destid", 0);
        getMoney();
        type = getIntent().getIntExtra("type", 0);
        Bundle bundle = new Bundle();
        bundle.putLong("destid", destid);
        if (0 == type){
            mTitles = new String[]{getResources().getString(R.string.redpacket_pt), getResources().getString(R.string.redpacket_kl)};
            Fragment fragment = new SendRedPTFragment();
            fragment.setArguments(bundle);
            mFragments.add(fragment);
        }else if (1 == type){
            //拼手气红包,幸运红包，口令红包，普通红包getResources().getString(R.string.Luc_kyred_envelope)
            mTitles = new String[]{getResources().getString(R.string.redpacket_psq),getResources().getString(R.string.Luc_kyred_envelope), getResources().getString(R.string.redpacket_pt), getResources().getString(R.string.redpacket_kl)};
//            SendRedGroupPTFragment
            Fragment fragment = new SendRedGroupPSQFragment();
            fragment.setArguments(bundle);

            //幸运红包
            Fragment fragment4 = new LuckyRedEnvelopeFragment();
            fragment4.setArguments(bundle);

            Fragment fragment2 = new SendRedGroupPTFragment();
            fragment2.setArguments(bundle);
            mFragments.add(fragment);
            mFragments.add(fragment4);
            mFragments.add(fragment2);
        }//SendRedKLFragment
        bundle.putLong("type", type);
        Fragment fragment3 = new SendRedKLFragment();
        fragment3.setArguments(bundle);

        mFragments.add(fragment3);
        ViewPager vp = (ViewPager) findViewById(R.id.vp);
        mAdapter = new MyPagerAdapter(getSupportFragmentManager());
        vp.setAdapter(mAdapter);

        /**自定义部分属性*/
        SlidingTabLayout tabLayout_2 = (SlidingTabLayout) findViewById(R.id.tl_2);
        tabLayout_2.setViewPager(vp);
        tabLayout_2.setOnTabSelectListener(this);

    }

    @Override
    public int getContentViewId() {
        return R.layout.activity_send_red_packet_select ;
    }

    private void getMoney(){
        mPgService.getBalance(ToolsUtils.getMyUserId()).subscribe(new AbsAPICallback<String>() {
            @Override
            protected void onResultError(ApiException ex) {
                showToast(getResources().getString(R.string.net_visit_exception));
            }

            @Override
            public void onNext(String s) {
                balance = Double.parseDouble(s);
//                tvBalance.setText("余额： "+s+"元");
                EventBus.getDefault().post(new EventBusTypeObject(EventBusType.REFRESH_TYPE_RED_MONEY,s,""));
            }
        });
    }

    @Override
    public void onTabSelect(int position) {

    }

    @Override
    public void onTabReselect(int position) {

    }

    private class MyPagerAdapter extends FragmentPagerAdapter {
        public MyPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public int getCount() {
            return null==mFragments?0:mFragments.size();
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return null==mTitles?"":mTitles[position];
        }

        @Override
        public Fragment getItem(int position) {
            return null==mFragments?null:mFragments.get(position);
        }
    }

}
